import Drawable from "./Drawable";
import Utils from "../utils/Utils";

export default class DrawableParser {

    static async get(id: string) {
        let path = `data/${id}.json`
        let drawable = await Utils.loadRes<Drawable>(path)
        if (!drawable.path) {
            drawable.path = []
            if (drawable._path) {
                for (let prop in drawable._path) {
                    drawable.path.push(cc.v2(parseInt(prop), drawable._path[prop]))
                }
                let len = 0
                for (let i = 1; i <= drawable.path.length; i++) {
                    len += cc.pDistance(drawable.path[i >= drawable.path.length ? 0 : i], drawable.path[i - 1])
                }
                drawable.len = len
            }
        }
        return drawable
    }
}